package com.nd.service.wx;

import cn.hutool.core.date.DateTime;
import com.nd.dao.AddressMapper;
import com.nd.dao.RegionMapper;
import com.nd.pojo.bean.*;
import com.nd.pojo.bo.wx.AddressAddBO;
import com.nd.pojo.vo.wx.AddressVO;
import com.nd.pojo.vo.wx.BaseRespWXVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: project2mall
 * @description:
 * @author: shit_code_builder
 * @create: 2021-06-07 23:02
 **/
@Service
public class AddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;


    public BaseRespWXVo addressList() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer id = user.getId();
        AddressExample example = new AddressExample();
        example.createCriteria().andUserIdEqualTo(id).andDeletedEqualTo(false);
        List<Address> addressList = addressMapper.selectByExample(example);
        List<AddressVO> addressVOS = new ArrayList<AddressVO>();
        for (Address address : addressList) {
            AddressVO build = AddressVO.builder().detailedAddress(address.getAddress()).id(address.getId()).isDefault(address.getIsDefault()).mobile(address.getMobile()).name(address.getName()).build();

            addressVOS.add(build);
        }

        return BaseRespWXVo.ok(addressVOS);
    }

    public BaseRespWXVo<List<Region>> selectRegin(Integer id) {

        RegionExample regionExample = new RegionExample();
        regionExample.createCriteria().andPidEqualTo(id);
        List<Region> regions = regionMapper.selectByExample(regionExample);
        if (regions.size()==0)return BaseRespWXVo.fail("查询失败");

    return BaseRespWXVo.ok(regions);
    }

    @Transactional
    public BaseRespWXVo<Integer> addAddress(AddressAddBO bo) {

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer id = user.getId();
        Date addTime = null;
        if (bo.getId()==0){
            addTime=DateTime.now();
        }

        Address build = Address.builder().name(bo.getName()).userId(user.getId()).provinceId(bo.getProvinceId()).cityId(bo.getCityId()).areaId(bo.getAreaId()).address(bo.getAddress()).addTime(addTime).updateTime(DateTime.now()).mobile(bo.getMobile()).isDefault(bo.isIsDefault()).deleted(false).build();

        AddressExample example1 = new AddressExample();
        example1.createCriteria().andUserIdEqualTo(id).andDeletedEqualTo(false);
        List<Address> addressList1 = addressMapper.selectByExample(example1);
        if (addressList1.isEmpty()){
            build.setIsDefault(true);
        }

        int i =1 ;
        if (bo.isIsDefault()){
            AddressExample example = new AddressExample();
            example.createCriteria().andIsDefaultEqualTo(true).andUserIdEqualTo(id).andDeletedEqualTo(false);
            List<Address> addressList = addressMapper.selectByExample(example);
            if (!addressList.isEmpty()){
                addressList.get(0).setIsDefault(false);
                i = addressMapper.updateByPrimaryKey(addressList.get(0));
            }
        }
        int insert=0;
        if (bo.getId()==0) {
           insert = addressMapper.insert(build);
        }else {
            build.setId(bo.getId());
            insert = addressMapper.updateByPrimaryKeySelective(build);
        }




        return BaseRespWXVo.ok(10);
    }

    public BaseRespWXVo delAddress(Integer id) {

        Address address = addressMapper.selectByPrimaryKey(id);
        address.setDeleted(true);
        if (address.getIsDefault()){
            address.setIsDefault(false);
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(address.getUserId()).andNameEqualTo(address.getName());
            List<Address> addressList = addressMapper.selectByExample(addressExample);
            if (addressList.size()!=0){
                addressList.get(0).setIsDefault(true);
                int i = addressMapper.updateByPrimaryKeySelective(addressList.get(0));
            }else {
                AddressExample example = new AddressExample();
                example.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(address.getUserId());
                List<Address> addressList1 = addressMapper.selectByExample(example);
                if (addressList1.size()!=0){
                    addressList1.get(0).setIsDefault(true);
                    addressMapper.updateByPrimaryKeySelective(addressList1.get(0));
                }
            }
        }
        int i = addressMapper.updateByPrimaryKeySelective(address);
    return BaseRespWXVo.ok();
    }
}
